package com.tang.subject.domain.handler.subject;

import com.tang.common.enums.DeleteEnum;
import com.tang.subject.common.enums.SubjectTypeEnum;
import com.tang.subject.domain.convert.BriefSubjectConverter;
import com.tang.subject.domain.entity.SubjectInfoBO;
import com.tang.subject.domain.entity.SubjectOptionBO;
import com.tang.subject.infra.basic.entity.pojo.SubjectBrief;
import com.tang.subject.infra.basic.service.SubjectBriefService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;


@Slf4j
@Service
public class BriefTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectBriefService subjectBriefService;

    /**
     * 获取题目类型
     *
     * @return com.tang.subject.common.enums.SubjectTypeEnum
     * @author Tang
     * @date 2024/2/4 14:16
     **/
    @Override
    public SubjectTypeEnum getSubjectType() {
        return SubjectTypeEnum.BRIEF;
    }

    /**
     * 添加题目
     *
     * @param subjectInfoBO 待插入的题目信息
     * @author Tang
     * @date 2024/2/4 14:22
     **/
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        SubjectBrief subjectBrief = BriefSubjectConverter.INSTANCE.convertBoToEntity(subjectInfoBO);
        subjectBrief.setSubjectId(subjectInfoBO.getId());
        subjectBrief.setIsDeleted(DeleteEnum.UN_DELETED.getCode());
        subjectBriefService.insert(subjectBrief);
    }

    /**
     * 查询对应题目的具体选项信息
     *
     * @param subjectId 题目id
     * @return com.tang.subject.domain.entity.SubjectOptionBO
     * @author Tang
     * @date 2024/2/5 17:42
     **/
    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectBrief subjectBrief = new SubjectBrief();
        subjectBrief.setSubjectId(subjectId);
        subjectBrief.setIsDeleted(DeleteEnum.UN_DELETED.getCode());
        List<SubjectBrief> subjectBriefs = subjectBriefService.queryListByCondition(subjectBrief);
        SubjectBrief brief = subjectBriefs.stream().findFirst().orElse(null);
        SubjectOptionBO optionBO = new SubjectOptionBO();
        optionBO.setSubjectAnswer(Optional.ofNullable(brief)
                .map(SubjectBrief::getSubjectAnswer)
                .orElse("略"));
        return optionBO;
    }
}
